﻿
namespace FXC.Web.Service
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using FXC.Web.Entity;


    // Implements application logic using the FXSystemEntities context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class FXSystemService : LinqToEntitiesDomainService<FXSystemEntities>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'City' query.
        public IQueryable<City> GetCity()
        {
            return this.ObjectContext.City;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Country' query.
        public IQueryable<Country> GetCountry()
        {
            return this.ObjectContext.Country.Where(c => c.IsActive.HasValue ? c.IsActive.Value : false);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Currency' query.
        public IQueryable<Currency> GetCurrency()
        {
            return this.ObjectContext.Currency;
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'FXCatagory' query.
        public IQueryable<FXCatagory> GetFXCatagory()
        {
            return this.ObjectContext.FXCatagory.Where(c => c.IsValid == true).OrderBy(c => c.SortOrder);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'FXProduct' query.
        public IQueryable<FXProduct> GetFXProduct()
        {
            return this.ObjectContext.FXProduct.Where(p => p.IsActive == true).OrderBy(p => p.FXProductName);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'State' query.
        public IQueryable<State> GetState()
        {
            return this.ObjectContext.State.Include("Cities");
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'User' query.
        public IQueryable<User> GetUser(string userInfo, string password)
        {
            return this.ObjectContext.User.Include("Country").Include("City").Include("UserFXProductSettings")
                .Where(u => ((u.Email == userInfo && u.Password == password) || (u.UserName == userInfo && u.Password == password)));
        }

        public void InsertUser(User user)
        {
            if ((user.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
            }
            else
            {
                this.ObjectContext.User.AddObject(user);
            }
        }

        public void UpdateUser(User currentUser)
        {
            this.ObjectContext.User.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));
        }

        public void DeleteUser(User user)
        {
            if ((user.EntityState == EntityState.Detached))
            {
                this.ObjectContext.User.Attach(user);
            }
            this.ObjectContext.User.DeleteObject(user);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'UserFXProductSetting' query.
        public IQueryable<UserFXProductSetting> GetUserFXProductSetting()
        {
            return this.ObjectContext.UserFXProductSetting;
        }

        public void InsertUserFXProductSetting(UserFXProductSetting userFXProductSetting)
        {
            if ((userFXProductSetting.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(userFXProductSetting, EntityState.Added);
            }
            else
            {
                this.ObjectContext.UserFXProductSetting.AddObject(userFXProductSetting);
            }
        }

        public void UpdateUserFXProductSetting(UserFXProductSetting currentUserFXProductSetting)
        {
            this.ObjectContext.UserFXProductSetting.AttachAsModified(currentUserFXProductSetting, this.ChangeSet.GetOriginal(currentUserFXProductSetting));
        }

        public void DeleteUserFXProductSetting(UserFXProductSetting userFXProductSetting)
        {
            if ((userFXProductSetting.EntityState == EntityState.Detached))
            {
                this.ObjectContext.UserFXProductSetting.Attach(userFXProductSetting);
            }
            this.ObjectContext.UserFXProductSetting.DeleteObject(userFXProductSetting);
        }
    }
}


